# _*_ coding : utf-8 _*_
# @Time : 2023/8/26 21:36
# @Author : 寻梦
# @File : 13_ip代理池的使用
# @Project : code
import random
import urllib.request

import ipapi
# 创建代理池
ip_proxy_pool=[]
ipandports=ipapi.getApis(5)
for ipandport in ipandports:
    ip_proxy_pool.append({"http":ipandport})

proxies=random.choice(ip_proxy_pool)

base_url="http://www.cip.cc/"
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/30"
}
request= urllib.request.Request(url=base_url,headers=headers)
handler=urllib.request.ProxyHandler(proxies=proxies)
opener=urllib.request.build_opener(handler)
response=opener.open(request)
content=response.read().decode("utf8")
print(content)